package cn.com.blueInfo.bpm.number.repository.impl;

import cn.com.blueInfo.bpm.common.entity.BaseEntity;
import cn.com.blueInfo.bpm.common.enums.BaseDeleted;
import cn.com.blueInfo.bpm.common.repository.BaseRepository;
import cn.com.blueInfo.bpm.number.entity.NumberRule;
import cn.com.blueInfo.bpm.number.enums.NumberRuleDefaultFlag;
import cn.com.blueInfo.bpm.number.mapper.NumberRuleMapper;
import cn.com.blueInfo.bpm.number.repository.NumberRuleRepository;
import cn.com.blueInfo.bpm.number.request.NumberRuleDTO;
import cn.com.blueInfo.core.entity.CurrentLoginUserInfo;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.toolkit.support.SFunction;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.extern.log4j.Log4j2;
import org.apache.commons.lang3.ObjectUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * 编号规则定义表 数据服务实现类
 * @author suxch
 * @since 2025-09-01 19:26:29
 */
@Log4j2
@Service
public class NumberRuleRepositoryImpl
        extends ServiceImpl<NumberRuleMapper, NumberRule>
        implements NumberRuleRepository, BaseRepository<NumberRule, NumberRuleDTO.QueryDTO> {

    @Autowired
    private CurrentLoginUserInfo currentLoginUserInfo;

    /**
     * 提供获取 CurrentLoginUserInfo 的方法（由实现类提供，避免接口中注入）
     * @return CurrentLoginUserInfo
     */
    @Override
    public CurrentLoginUserInfo getCurrentLoginUserInfo() {
        return currentLoginUserInfo;
    }

    /**
     * 新增编号规则定义
     * @param numberRule 编号规则定义参数
     */
    @Override
    public Integer createNumberRule(NumberRule numberRule) {
        return sqlExecuteResult(save(numberRule));
    }

    /**
     * 批量新增编号规则定义
     * @param numberRuleList 编号规则定义参数列表
     */
    @Override
    public Integer batchCreateNumberRule(List<NumberRule> numberRuleList) {
        return sqlExecuteResult(saveBatch(numberRuleList), numberRuleList.size());
    }

    /**
     * 删除编号规则定义
     * @param numberRuleId 编号规则定义ID
     */
    @Override
    public Integer deleteNumberRule(Long numberRuleId) {
        NumberRule numberRule = new NumberRule();
        numberRule.setId(numberRuleId);
        numberRule.setDeleted(BaseDeleted.DELETED);
        return sqlExecuteResult(removeById(numberRule));
    }

    /**
     * 批量删除编号规则定义
     * @param numberRuleIdList 编号规则定义ID列表
     */
    @Override
    public Integer batchDeleteNumberRule(List<Long> numberRuleIdList) {
        List<NumberRule> numberRuleList = new ArrayList<>();
        for (Long numberRuleId : numberRuleIdList) {
            NumberRule numberRule = new NumberRule();
            numberRule.setId(numberRuleId);
            numberRule.setDeleted(BaseDeleted.DELETED);
            numberRuleList.add(numberRule);
        }
        return sqlExecuteResult(removeBatchByIds(numberRuleList), numberRuleList.size());
    }

    /**
     * 更新部分编号规则定义
     * @param numberRule 编号规则定义参数
     */
    @Override
    public Integer updateNumberRulePartial(NumberRule numberRule) {
        validateUpdateId(numberRule.getId());
        LambdaUpdateWrapper<NumberRule> updateWrapper = new LambdaUpdateWrapper<>();
        updateWrapper.eq(NumberRule::getId, numberRule.getId());
        return sqlExecuteResult(update(updateWrapper));
    }

    /**
     * 更新全部编号规则定义
     * @param numberRule 编号规则定义参数
     */
    @Override
    public Integer updateNumberRule(NumberRule numberRule) {
        validateUpdateId(numberRule.getId());
        return sqlExecuteResult(updateById(numberRule));
    }

    /**
     * 查询一条编号规则定义
     * @param numberRuleId 编号规则定义ID
     */
    @Override
    public NumberRule getNumberRuleById(Long numberRuleId) {
        LambdaQueryWrapper<NumberRule> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(NumberRule::getId, numberRuleId);
        return getOne(queryWrapper);
    }

    /**
     * 分页查询编号规则定义
     * @param numberRuleQueryDTO 编号规则定义参数
     */
    @Override
    public Page<NumberRule> getNumberRulePage(NumberRuleDTO.QueryDTO numberRuleQueryDTO) {
        Page<NumberRule> numberRulePage = new Page<>(
                numberRuleQueryDTO.getPage(), numberRuleQueryDTO.getSize());
        LambdaQueryWrapper<NumberRule> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper(queryWrapper, numberRuleQueryDTO);

        BaseDeleted deleted = numberRuleQueryDTO.getDeleted();
        if (ObjectUtils.isNotEmpty(deleted) && deleted.equals(BaseDeleted.DELETED)) {
            return baseMapper.selectDeletePage(numberRulePage, queryWrapper);
        } else {
            return page(numberRulePage, queryWrapper);
        }
    }

    /**
     * 根据条件查询 编号规则定义 数量
     * @param numberRuleQueryDTO 编号规则定义参数
     */
    @Override
    public Long getNumberRuleCount(NumberRuleDTO.QueryDTO numberRuleQueryDTO) {
        LambdaQueryWrapper<NumberRule> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper(queryWrapper, numberRuleQueryDTO);

        BaseDeleted deleted = numberRuleQueryDTO.getDeleted();
        if (ObjectUtils.isNotEmpty(deleted) && deleted.equals(BaseDeleted.DELETED)) {
            return baseMapper.selectDeleteCount(queryWrapper);
        } else {
            return count(queryWrapper);
        }
    }

    /**
     * 根据条件查询 编号规则定义
     * @param numberRuleQueryDTO 编号规则定义参数
     */
    @Override
    public List<NumberRule> getNumberRuleList(NumberRuleDTO.QueryDTO numberRuleQueryDTO) {
        LambdaQueryWrapper<NumberRule> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper(queryWrapper, numberRuleQueryDTO);

        BaseDeleted deleted = numberRuleQueryDTO.getDeleted();
        if (ObjectUtils.isNotEmpty(deleted) && deleted.equals(BaseDeleted.DELETED)) {
            return baseMapper.selectDeleteList(queryWrapper);
        } else {
            return list(queryWrapper);
        }
    }

    private void queryWrapper(LambdaQueryWrapper<NumberRule> queryWrapper,
                              NumberRuleDTO.QueryDTO numberRuleQueryDTO) {

        String ruleCode = numberRuleQueryDTO.getRuleCode();
        queryWrapper.eq(StringUtils.isNotBlank(ruleCode), NumberRule::getRuleCode, ruleCode);

        String ruleName = numberRuleQueryDTO.getRuleName();
        queryWrapper.eq(StringUtils.isNotBlank(ruleName), NumberRule::getRuleName, ruleName);

        String bizType = numberRuleQueryDTO.getBizType();
        queryWrapper.eq(StringUtils.isNotBlank(bizType), NumberRule::getBizType, bizType);

        String description = numberRuleQueryDTO.getDescription();
        queryWrapper.eq(StringUtils.isNotBlank(description), NumberRule::getDescription, description);

        NumberRuleDefaultFlag defaultFlag = numberRuleQueryDTO.getDefaultFlag();
        queryWrapper.eq(ObjectUtils.isNotEmpty(defaultFlag), NumberRule::getDefaultFlag, defaultFlag);

        addQueryWrapper(queryWrapper, numberRuleQueryDTO, getCommonColumn());

    }

    private Map<String, SFunction<NumberRule, ?>> getCommonColumn() {
        return BaseEntity.getCommonColumn(
                NumberRule::getOrderNumber,
                NumberRule::getTenantId,
                NumberRule::getApplicationId,
                NumberRule::getStatus,
                NumberRule::getDeleted,
                NumberRule::getDeleteUserId,
                NumberRule::getDeleteTime,
                NumberRule::getRemark,
                NumberRule::getCreateUserId,
                NumberRule::getCreateTime,
                NumberRule::getUpdateUserId,
                NumberRule::getUpdateTime,
                NumberRule::getExtend1,
                NumberRule::getExtend2,
                NumberRule::getExtend3);
    }

}
